import 'dart:convert';
/// amount : 0
/// creatTime : "2023-12-29T06:38:21.835Z"
/// id : 0
/// isDelete : true
/// itemList : [{"count":0,"id":0,"isDelete":true,"mark":"string","name":"string","price":0,"size":"string","standards":"string","stockId":0}]
/// realAmount : 0
/// recordTime : "2023-12-29T06:38:21.835Z"
/// stockPhone : "string"
/// stockUserName : "string"
/// userId : 0

PurchaseEntity purchassEntityFromJson(String str) => PurchaseEntity.fromJson(json.decode(str));
String purchassEntityToJson(PurchaseEntity data) => json.encode(data.toJson());
class PurchaseEntity {
  PurchaseEntity({
      this.amount, 
      this.creatTime, 
      this.id, 
      this.isDelete, 
      this.itemList, 
      this.realAmount,
      this.payAmount,
      this.recordTime,
      this.stockPhone, 
      this.stockUserName, 
      this.userId,
      this.fullPay = false,
  });

  PurchaseEntity.fromJson(dynamic json) {
    amount = json['amount'];
    creatTime = json['creatTime'];
    id = json['id'];
    isDelete = json['isDelete'];
    if (json['itemList'] != null) {
      itemList = [];
      json['itemList'].forEach((v) {
        itemList?.add(PurchaseItem.fromJson(v));
      });
    }
    realAmount = json['realAmount'];
    payAmount = json['payAmount'];
    recordTime = json['recordTime'];
    stockPhone = json['stockPhone'];
    stockUserName = json['stockUserName'];
    userId = json['userId'];
    fullPay = json['fullPay'] ?? false;
  }
  num? amount;
  String? creatTime;
  num? id;
  bool? isDelete;
  List<PurchaseItem>? itemList;
  num? realAmount;
  num? payAmount;
  String? recordTime;
  String? stockPhone;
  String? stockUserName;
  num? userId;
  bool fullPay = false;

  num get totalCount {
    if (itemList == null || itemList!.isEmpty) {
      return 0;
    }
    num totalCount = 0;
    for (PurchaseItem item in itemList!) {
      if (item.count != null) {
        totalCount += item.count!;
      }
    }
    return totalCount;
  }

  num get totalAmount {
    if(realAmount != null) {
      return realAmount!;
    }
    if (amount != null) {
      return amount!;
    }
    if (itemList == null || itemList!.isEmpty) {
      return 0;
    }
    num totalAmount = 0;
    for (PurchaseItem item in itemList!) {
      if (item.count != null && item.price != null) {
        totalAmount += item.count! * item.price!;
      }
    }
    return totalAmount;
  }


PurchaseEntity copyWith({  num? amount,
  String? creatTime,
  num? id,
  bool? isDelete,
  List<PurchaseItem>? itemList,
  num? realAmount,
  num? payAmount,
  String? recordTime,
  String? stockPhone,
  String? stockUserName,
  num? userId,
  bool? fullPay,
}) => PurchaseEntity(  amount: amount ?? this.amount,
  creatTime: creatTime ?? this.creatTime,
  id: id ?? this.id,
  isDelete: isDelete ?? this.isDelete,
  itemList: itemList ?? this.itemList,
  realAmount: realAmount ?? this.realAmount,
  payAmount: payAmount ?? this.payAmount,
  recordTime: recordTime ?? this.recordTime,
  stockPhone: stockPhone ?? this.stockPhone,
  stockUserName: stockUserName ?? this.stockUserName,
  userId: userId ?? this.userId,
  fullPay: fullPay ?? this.fullPay,
);
  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['amount'] = amount;
    map['creatTime'] = creatTime;
    map['id'] = id;
    map['isDelete'] = isDelete;
    if (itemList != null) {
      map['itemList'] = itemList?.map((v) => v.toJson()).toList();
    }
    map['realAmount'] = realAmount;
    map['payAmount'] = payAmount;
    map['recordTime'] = recordTime;
    map['stockPhone'] = stockPhone;
    map['stockUserName'] = stockUserName;
    map['userId'] = userId;
    map['fullPay'] = fullPay;
    return map;
  }

}

/// count : 0
/// id : 0
/// isDelete : true
/// mark : "string"
/// name : "string"
/// price : 0
/// size : "string"
/// standards : "string"
/// stockId : 0

PurchaseItem itemListFromJson(String str) => PurchaseItem.fromJson(json.decode(str));
String itemListToJson(PurchaseItem data) => json.encode(data.toJson());
class PurchaseItem {
  PurchaseItem({
      this.count, 
      this.id, 
      this.isDelete, 
      this.mark, 
      this.name, 
      this.price, 
      this.size, 
      this.standards, 
      this.material,
      this.stockId,});

  PurchaseItem.fromJson(dynamic json) {
    count = json['count'];
    id = json['id'];
    isDelete = json['isDelete'];
    mark = json['mark'];
    name = json['name'];
    price = json['price'];
    size = json['size'];
    standards = json['standards'];
    material = json['material'];
    stockId = json['stockId'];
  }
  num? count;
  num? id;
  bool? isDelete;
  String? mark;
  String? name;
  num? price;
  String? size;
  String? standards;
  String? material;
  num? stockId;
PurchaseItem copyWith({  num? count,
  num? id,
  bool? isDelete,
  String? mark,
  String? name,
  num? price,
  String? size,
  String? standards,
  String? material,
  num? stockId,
}) => PurchaseItem(  count: count ?? this.count,
  id: id ?? this.id,
  isDelete: isDelete ?? this.isDelete,
  mark: mark ?? this.mark,
  name: name ?? this.name,
  price: price ?? this.price,
  size: size ?? this.size,
  standards: standards ?? this.standards,
  material: material ?? this.material,
  stockId: stockId ?? this.stockId,
);
  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['count'] = count;
    map['id'] = id;
    map['isDelete'] = isDelete;
    map['mark'] = mark;
    map['name'] = name;
    map['price'] = price;
    map['size'] = size;
    map['standards'] = standards;
    map['material'] = material;
    map['stockId'] = stockId;
    return map;
  }

}